package com.example.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class LockAlternatorDemo {

    // 共享锁对象
    private static final WandyLock lock = new WandyLock();
    // 控制执行顺序的标志位
    private static boolean t1Turn = true;


    // 创建包含两个固定线程的线程池
    private ExecutorService executorService = Executors.newFixedThreadPool(2);



    public static void main(String[] args) {
        // 提交两个任务到线程池，但让它们顺序执行避免竞争
        //executorService.submit(new BiasLockTask("Thread-1"));
        //executorService.submit(new BiasLockTask("Thread-2"));

        Thread t1 = new Thread(() -> {
            try {
                while (true) {
                    synchronized (lock) {
                        // 等待自己的回合
                        while (!t1Turn) {
                            lock.wait();
                        }

                        // 执行任务
                        System.out.println("Thread-1 获得了锁并执行任务");
                        Thread.sleep(5000); // 模拟工作

                        // 交接控制权给Thread-2
                        t1Turn = false;
                        lock.notifyAll(); // 唤醒其他等待的线程
                    }
                }
            } catch (InterruptedException e) {
                System.out.println("Thread-1 interrupted");
                Thread.currentThread().interrupt();
            }
        });
        t1.setName("wandy-1");
        t1.start();

        Thread t2 = new Thread(() -> {
            try {
                while (true) {
                    synchronized (lock) {
                        // 等待自己的回合
                        while (t1Turn) {
                            lock.wait();
                        }

                        // 执行任务
                        System.out.println("Thread-2 获得了锁并执行任务");
                        Thread.sleep(5000); // 模拟工作

                        // 交接控制权给Thread-1
                        t1Turn = true;
                        lock.notifyAll(); // 唤醒其他等待的线程
                    }
                }
            } catch (InterruptedException e) {
                System.out.println("Thread-2 interrupted");
                Thread.currentThread().interrupt();
            }
        });
        t2.setName("wandy-2");
        t2.start();
    }

    // 偏向锁任务类 - 避免锁竞争
    private class BiasLockTask implements Runnable {
        private final String threadName;

        public BiasLockTask(String threadName) {
            this.threadName = threadName;
        }

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                synchronized (lock) {
                    // 每个线程获取锁后执行较长时间的任务
                    // 这样JVM会倾向于将锁偏向于当前线程
                    System.out.println(threadName + " 获得了锁");
                    try {
                        // 执行较长时间的操作，保持锁的持有
                        Thread.sleep(5000); // 5秒，足够让偏向锁生效
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        return;
                    }
                    System.out.println(threadName + " 释放了锁");
                }

                // 短暂休息后再尝试获取锁
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
        }
    }

    public static class WandyLock {

    }
}
